package com.practice.xw.controller;


import com.practice.xw.entuty.BaseResponse;
import com.practice.xw.entuty.CobraResultCodeConstant;
import com.practice.xw.entuty.ResultCode;
import com.practice.xw.handler.ProxyHandler;
import com.practice.xw.service.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author ：xiongwei
 * @date ：Created in 2021/2/28 23:15
 */
@Slf4j
@RestController
@RequestMapping("/test")
@Api(value = "TestController | 测试接口")
public class TestController {

    @Autowired
    private DemoService demoService;

    @GetMapping(value = "/testPage")
    @ApiOperation(value = "测试页面 ", notes = "测试接口")
    public BaseResponse testPage() {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(), "testPage");
        log.info("testPage = {}", "测试页面");
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/receiveData")
    @ApiOperation(value = "接收数据 ", notes = "接收的是识别结果字符串")
    @ApiImplicitParam(paramType = "query", name = "data", value = "数据", required = false, dataType = "String")
    public BaseResponse receiveData(@RequestParam(value = "data", required = true, defaultValue = "") String receiveData) {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(), receiveData);
        log.debug("receiveData = {}", receiveData);
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/testAsync")
    @ApiOperation(value = "测试异步 ", notes = "接收的是识别结果字符串")
    @ApiImplicitParam(paramType = "query", name = "asyncName", value = "异步名称", required = false, dataType = "String")
    public BaseResponse testAsync(@RequestParam(value = "asyncName", required = true, defaultValue = "") String asyncName) {
        ResultCode resultCode = new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(), asyncName);
        log.info("testAsync = {}", asyncName);
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/proxyHandlerTest")
    @ApiOperation(value = "测试动态代理", notes = "动态代理")
    public BaseResponse proxyHandlerTest() {
        InvocationHandler handler = new ProxyHandler(demoService);
        DemoService proxyDemoService = (DemoService) Proxy.newProxyInstance(demoService.getClass().getClassLoader(), demoService.getClass().getInterfaces(), handler);
        log.info("int = {}",proxyDemoService.getMessage(3));
        try {
            log.info("long = {}",proxyDemoService.getLongMessage(-1L));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new BaseResponse(new ResultCode(CobraResultCodeConstant.SUCCESS.getCode(), "proxyHandlerTest"));
    }

}


